package com.hualan.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.PsychologyTestClassifyMapper;
import com.hualan.sfzy.mapper.PsychologyTestMapper;
import com.hualan.sfzy.model.entity.Answer;
import com.hualan.sfzy.model.entity.PsychologyTest;
import com.hualan.sfzy.model.entity.PsychologyTestClassify;
import com.hualan.sfzy.service.BaseService;
import com.hualan.sfzy.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PsychologyTestClassifyServiceImpl implements BaseService<PsychologyTestClassify> {
    @Autowired
    private PsychologyTestClassifyMapper psychologyTestClassifyMapper;
    @Autowired
    private PsychologyTestMapper psychologyTestMapper;

    /*添加*/
    @Override
    public int add(PsychologyTestClassify entity) {
        return psychologyTestClassifyMapper.insert(entity);
    }

    /*编辑*/
    @Override
    public int edit(PsychologyTestClassify entity) {
        return psychologyTestClassifyMapper.update(entity);
    }

    /*删除*/
    @Override
    @Transactional
    public int remove(Integer id) {
        // 1. 先根据分类ID查询分类信息
        PsychologyTestClassify Classify = psychologyTestClassifyMapper.selectById(id);
        // 添加空值检查
        if (Classify == null) {
            throw new RuntimeException("ID为" + id + "的分类不存在");
        }
        // 2. 直接统计有效测试数量
        int testCount = psychologyTestClassifyMapper.countTestsByClassifyId(id);
        // 3. 无关联测试时执行删除
        if (testCount == 0) {
            return psychologyTestClassifyMapper.delete(id);
        }
            throw new RuntimeException(Classify.getClassifyName() + "分类下存在" + testCount + "个有效测评，不允许删除");
    }

    /*批量删除*/
    @Override
    @Transactional
    public int removeBatch(Integer[] ids) {
        // 1. 前置检查所有分类
        for (Integer id : ids) {
            PsychologyTestClassify classify = psychologyTestClassifyMapper.selectById(id);
            if (classify == null || classify.getDel() == 1) {
                throw new RuntimeException("ID为" + id + "的分类不存在或已被删除");
            }
            int testCount = psychologyTestClassifyMapper.countTestsByClassifyId(id);
            if (testCount > 0) {
                throw new RuntimeException(classify.getClassifyName() + "分类下存在" + testCount + "个有效测评，不允许删除");
            }
        }
        // 2. 统一执行批量删除
        return psychologyTestClassifyMapper.deleteBatch(ids);
    }

    /*查询全部*/
    @Override
    public List<PsychologyTestClassify> find() {
        return psychologyTestClassifyMapper.select(null);
    }

    /*分页查询*/
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, PsychologyTestClassify entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<PsychologyTestClassify> list = psychologyTestClassifyMapper.select(entity);
        PageInfo<PsychologyTestClassify> pageInfo = new PageInfo<>(list); // 分页信息对象
        Map<String, Object> map = new HashMap<>();
        // map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    /*根据id查询*/
    @Override
    public PsychologyTestClassify findById(Integer id) {
        return psychologyTestClassifyMapper.selectById(id);
    }

    /*查出所有的分类名*/
        public List<Map<String, Object>> selectAllClassifyName(){
            return psychologyTestClassifyMapper.selectAllClassifyName();
        }


    /*根据分类名查询*/
    public Map<String, Object> selectByClassifyName(Integer pageNum, Integer pageSize,String classifyName){
        PageHelper.startPage(pageNum, pageSize);
        List<PsychologyTestClassify> list = psychologyTestClassifyMapper.selectByClassifyName(classifyName);
        PageInfo<PsychologyTestClassify> pageInfo = new PageInfo<>(list); // 分页信息对象
        Map<String, Object> map = new HashMap<>();
        // map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    /*根据分类id*/
    public Integer selectByClassifyId(Integer classifyId){
        return psychologyTestClassifyMapper.countTestsByClassifyId(classifyId);
    }
    /*根据分类ID获取该分类下的所有测试*/
    public List<PsychologyTest> selectTestsByClassifyId(Integer classifyId){
        return psychologyTestClassifyMapper.selectTestsByClassifyId(classifyId);
    }

}
